package com.sparrow.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.sparrow.constant.*;
import com.sparrow.constant.FILE.SIZE;
import com.sparrow.core.Log;

public class FlashUtil {
	private final String youku = "http:\\/\\/player.youku.com\\/player.php.*?sid\\/(.*?)\\/v\\.swf";
	private final String sohu = "http:\\/\\/share.vrs.sohu.com\\/([0-9]*?)\\/v\\.swf.*";

	private final String youkuAPI = "http://v.youku.com/player/getPlayList/VideoIDS/%1$s";
	private final String sohuapi = "http://hot.vrs.sohu.com/vrs_flash.action?vid=%1$s";

	private static FlashUtil d3FalshUtil = new FlashUtil();

	public static FlashUtil getInstance() {
		return d3FalshUtil;
	}

	// http://player.youku.com/player.php/Type/Folder/Fid/17354925/Ob/1/Pt/0/sid/XMzgwODczNDY4/v.swf
	private final Pattern youkuPatter = Pattern.compile(this.youku,
			REGEX.OPTION);

	// http://share.vrs.sohu.com/637677/v.swf&autoplay=false
	private final Pattern sohuPatter = Pattern.compile(this.sohu, REGEX.OPTION);

	private final Pattern youkuPic = Pattern.compile("\"logo\":\"(.*?)\"",
			REGEX.OPTION);
	private final Pattern sohuPic = Pattern.compile("\"coverImg\":\"(.*?)\"",
			REGEX.OPTION);

	public String getThumbnailUrl(String swfSrc) {
		// flash API
		String flashAPI = null;
		// 接口返回结果
		String result = null;
		// 缩略图
		String flashThumbnailUrl = null;
		try {

			// 优酷
			Matcher youkuMatcher = this.youkuPatter.matcher(swfSrc);
			// 搜狐
			Matcher sohuMatcher = this.sohuPatter.matcher(swfSrc);
			if (youkuMatcher.find()) {
				result = HttpClient.get(String.format(this.youkuAPI,
						youkuMatcher.group(1)));
				Matcher youkuPicMatcher = youkuPic.matcher(result);
				if (youkuPicMatcher.find()) {
					flashThumbnailUrl = youkuPicMatcher.group(1).replace("\\",
							"");
				}
			}
			// 搜狐
			else if (sohuMatcher.find()) {
				flashAPI = String.format(sohuapi, sohuMatcher.group(1));
				result = HttpClient.get(flashAPI);
				Matcher sohuPicMatch = this.sohuPic.matcher(result);
				if (sohuPicMatch.find()) {
					flashThumbnailUrl = sohuPicMatch.group(1);
				}
			}

			if (!StringUtil.isNullOrEmpty(flashThumbnailUrl)) {
				if (HttpClient.getResponseCode(flashThumbnailUrl) != 200) {
					return null;
				}
			}
			return flashThumbnailUrl;
		} catch (Exception e) {
			Log.getInstance().error(e);
			return null;
		}
	}

	/**
	 * 获取播放形式的flash HTML
	 * 
	 * @param flashUrl
	 * @param size
	 * @return
	 */
	public String getPlayHtml(String flashUrl, String size) {
		// 只显示第一个 flash
		String thumbnail = JSUtil.decodeURIComponent(flashUrl.split("\\#")[1])
				+ ".jpg";

		if (size.equals(SIZE.SMALL)) {
			thumbnail = thumbnail.replace(FILE.SIZE.BIG, FILE.SIZE.SMALL);
		}
		return String
				.format("<div class=\"flash\"><img onload=\"init_play(this,'%2$s');\" src=\"%1$s\"/></div>",
						thumbnail, flashUrl);
	}

	/**
	 * 视频的html
	 * 
	 * 因为.jpg形式无法编码，使视频无法播放，故此处过滤掉
	 * 
	 * 上传 时使用
	 * 
	 * @param flashUrl
	 * @param thumbnail
	 * @return
	 */
	public String getHtml(String flashUrl, int imageId) {
		String thumbnailUrl = this.getThumbnailUrl(flashUrl);
		if (StringUtil.isNullOrEmpty(thumbnailUrl)) {
			return null;
		}
		String originPath = FileUtil.getInstance().getBreakUpPath(imageId,
				".jpg", false, SIZE.ORIGIN);
		String bigPath = originPath.replace(FILE.SIZE.ORIGIN, FILE.SIZE.BIG);
		String middlePath = originPath.replace(FILE.SIZE.ORIGIN,
				FILE.SIZE.MIDDLE);
		String smallPath = originPath
				.replace(FILE.SIZE.ORIGIN, FILE.SIZE.SMALL);

		String logoWaterFile = Config
				.getValueInCache(CONFIG.RESOURCE_PHYSICAL_PATH)
				+ "/system/images/logo.png";
		HttpClient.downloadFile(thumbnailUrl, originPath);
		try {
			ImageUtil.makeThumbnail(originPath, bigPath, 480, -1,
					logoWaterFile, false);
			ImageUtil.makeThumbnail(originPath, middlePath, 240, -1,
					logoWaterFile, false);
			ImageUtil.makeThumbnail(originPath, smallPath, 180, 135,
					logoWaterFile, true);

		} catch (Exception e) {
			Log.getInstance().error(e);
		}
		thumbnailUrl = FileUtil.getInstance().getBreakUpPath(
				Integer.valueOf(imageId), ".jpg", true, SIZE.BIG);

		String videoHtml = String
				.format("<embed src=\"%1$s\" quality=\"high\" wmode=\"opaque\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"500\" height=\"500\"/>",
						flashUrl
								+ "#"
								+ JSUtil.encodeURIComponent(thumbnailUrl
										.substring(0,
												thumbnailUrl.lastIndexOf('.'))));
		return videoHtml;
	}
}
